package com.aaa.security;

import com.aaa.entity.ResultData;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class AuthenticationEntryPointConfig implements AuthenticationEntryPoint {
    /**
     *
     * @param httpServletRequest 请求
     * @param httpServletResponse 响应
     * @param e
     * @throws IOException
     * @throws ServletException
     */
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        PrintWriter writer = httpServletResponse.getWriter();
        int status = httpServletResponse.getStatus();
        String message = e.getMessage();
        ResultData resultData = new ResultData(status,message,null);
        writer.print(JSON.toJSON(new ResultData(status,message,e.toString())));
        writer.flush();
        writer.close();
    }


}
